﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ITAP.ShprossesBuilder.Modelling.Components.Constraints
{
    [Serializable]
    public class RemoveUnnecessaryShprossEndToEndConnector : IConstraint
    {
        #region IConstraint Members

        public bool Check(IComponent component)
        {
            return false;
        }

        public void Fix(IComponent component)
        {
            if (component is Shpross)
            {
                Shpross shpross = component as Shpross;

                List<IComponent> shprossEndToEnd = new List<IComponent>();
                if (shpross.StartConnectedComponent is ShprossEndToEndConnector)
                {
                    shprossEndToEnd = shpross.StartConnectedComponent.ConnectedComponents.GetComponentsByType(typeof(ShprossEndToEndConnector));
                    if (shprossEndToEnd.Count == 0)
                    {
                        shpross.RemoveInnerComponent(shpross.StartConnectedComponent);
                    }
                }
                if (shpross.EndConnectedComponent is ShprossEndToEndConnector)
                {
                    shprossEndToEnd = shpross.EndConnectedComponent.ConnectedComponents.GetComponentsByType(typeof(ShprossEndToEndConnector));
                    if (shprossEndToEnd.Count == 0)
                    {
                        shpross.RemoveInnerComponent(shpross.EndConnectedComponent);
                    }
                }
            }
        }

        private bool enabled = true;
        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
            }
        }

        public ConstraintAction Action
        {
            get { return ConstraintAction.Fix; }
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            RemoveUnnecessaryShprossEndToEndConnector clone = new RemoveUnnecessaryShprossEndToEndConnector();
            clone.Enabled = this.enabled;
            return clone;
        }

        #endregion
    }
}
